package nancal.mp.db.mongo.entity.dev

import nancal.mp.db.mongo.JenkinsJobStatusEnum
import nancal.mp.db.mongo.JenkinsLanguageEnum
import nancal.mp.db.mongo.MicroServiceDeployEnvEnum
import nbcp.base.db.IdCodeName
import nbcp.base.db.IdName
import nbcp.base.db.IdUrl
import nbcp.base.db.annotation.Cn
import nbcp.base.db.annotation.DbEntityGroup
import nbcp.base.db.annotation.DbEntityIndex
import nbcp.base.db.annotation.DbEntityIndexes
import nbcp.base.extend.Slice
import nbcp.base.model.BaseMarkEntity
import org.springframework.data.mongodb.core.mapping.Document
import java.time.LocalDateTime

@Document
@DbEntityGroup("dev")
@DbEntityIndexes(DbEntityIndex("createAt", unique = false))
data class JenkinsLog constructor(
    var status: JenkinsJobStatusEnum = JenkinsJobStatusEnum.None,
    var stages: MutableList<JenkinsLogItemData> = mutableListOf(),
    var gitData: GitData? = null,
    var sourceData: SourceData? = null,
    var buildInfo: JenkinsBuildInfo = JenkinsBuildInfo(),
    var buildVersion: JenkinsBuildDetailInfo = JenkinsBuildDetailInfo(),
    var createBy: IdName = IdName(),
) : BaseMarkEntity() {
    val currentGitFolder: GitFolderData
        get() {
            if (this.gitData == null) {
                throw RuntimeException("找不到 gitData")
            }
            return this.gitData!!.folders.first { it.path == this.buildInfo.buildFolder && it.name == this.buildInfo.appName }
        }

    val clusterCode: String
        get() {
            return this.buildInfo.clusterCode
        }

    val language: JenkinsLanguageEnum
        get() {
            return this.gitData?.language ?: this.sourceData?.language ?: throw RuntimeException("找不到源数据")
        }

    val languageVersion: String
        get() {
            return this.gitData?.languageVersion ?: this.sourceData?.languageVersion
            ?: throw RuntimeException("找不到源数据")
        }

    val productLine: IdCodeName
        get() {
            return this.gitData?.productLine ?: this.sourceData?.productLine ?: throw RuntimeException("找不到源数据")
        }
}


data class JenkinsLogItemData(
    var name: String = "",
    var successful: Boolean? = null,
    var content: String = ""
)


open class JenkinsBuildInfo(
    @Cn("git分支") var gitBranch: String = "",
    @Cn("构建文件夹") var buildFolder: String = "",

    /**
     * 对于后端是 spring.application.name , 对于前端是 虚拟目录
     */
    @Cn("注册名") var appName: String = "",
    var location: String = "",
    @Cn("注册中文名") var appCnName: String = "",

    /**
     * 部署环境,是指： AppDeploySetting.code
     */
    @Cn("部署环境")
    var targetEnv: String = "",

    /**
     * dev,test,main
     */
    @Cn("运行环境")
    var profile: String = "",

    @Cn("注册中心")
    var msDeployEnvEnum: MicroServiceDeployEnvEnum? = null,
//    var envServerHost:String = "",
    @Cn("K8s服务名,也是镜像名")
    var serviceName: String = "",       // == K8s Service Name

    /**
     * 镜像的前缀
     */
    var imageTagPrefix: String = "",

    @Cn("Jenkins构建版本号")
    var buildNumber: Int = 0,       // jobName 版本信息
    @Cn("集群名")
    var clusterCode: String = "",     //集群名称
    var isLib: Boolean = false
) {
    val isApp: Boolean
        get() {
            return !isLib
        }

    init {
        var tidy: ((String, String) -> String) = tidy@{ it, def ->
            var v2 = it;
            if (v2.startsWith("/")) {
                v2.substring(1)
            }

            if (v2.endsWith("/")) {
                v2.Slice(0, -1)
            }

            if (v2 == "/" || v2 == "") {
                v2 = def
            }
            return@tidy v2;
        }


        this.buildFolder = tidy(this.buildFolder, "./");
        this.appName = tidy(this.appName, "/");
    }
}


data class SourceData(
    var zipFile: IdUrl = IdUrl(),
    @Cn("集群") var clusterCode: String = "",
    var language: JenkinsLanguageEnum = JenkinsLanguageEnum.Java,
    @Cn("开发语言的版本，如 1.8,11,17") var languageVersion: String = "",
    var productLine: IdCodeName = IdCodeName(),
    var remark: String = ""
)


data class JenkinsBuildDetailInfo(
    var dockerImageVersion: String = "",
    @Cn("Git提交Id") var gitCommitId: String = "",
    @Cn("Git提交时间") var gitCommitAt: String = "",
    var createAt: LocalDateTime = LocalDateTime.now()
)
